﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Aop.Api.Response;
using Aop.Api.Domain;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class AlipayAddDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        public static string conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”

        /// <summary>
        /// 添加扫码支付记录
        /// </summary>
        /// <param name="queryRes"></param>
        /// <returns></returns>
        public static int addNativeRecord(AlipayTradeQueryResponse queryRes,string cardno)
        {
            List<TradeFundBill> list = queryRes.FundBillList;
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter("@OutTradeNo",queryRes.OutTradeNo),
                new SqlParameter("@TradeNo",queryRes.TradeNo),
                new SqlParameter("@cardno",cardno),
                new SqlParameter("@BuyerLogonId",queryRes.BuyerLogonId),
                new SqlParameter("@BuyerUserId",queryRes.BuyerUserId),
                new SqlParameter("@TotalAmount",queryRes.TotalAmount),
                new SqlParameter("@ReceiptAmount",queryRes.ReceiptAmount),
                new SqlParameter("@BuyerPayAmount",queryRes.BuyerPayAmount),
                new SqlParameter("@PointAmount",queryRes.PointAmount),
                new SqlParameter("@InvoiceAmount",queryRes.InvoiceAmount),
                new SqlParameter("@FundChannel",list[0].FundChannel),
                new SqlParameter("@TradeStatus",queryRes.TradeStatus),
                new SqlParameter("@SendPayDate",queryRes.SendPayDate)
            };
            string sqlstr =
"if not exists(select 1 from alipay_record where TradeNo=@TradeNo) insert into alipay_record values(@OutTradeNo,@TradeNo,@cardno,@BuyerLogonId,@BuyerUserId,@TotalAmount,@ReceiptAmount,@BuyerPayAmount,@PointAmount,@InvoiceAmount,@FundChannel,@TradeStatus,@SendPayDate,0)";
            return SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sqlstr, sp);
        }
    }
}
